<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/11/23
 * Time: 17:49
 */
namespace Home\Model;
use Think\Model;
class UserModel  extends Model
{
    protected $tableName = "p_user";
    protected $_validate = array(
        array('verify','require','验证码不能为空'),
        array('name','','名字不能为空',0,'unique',1),
        array('value',array(1,2,3),'ֵ',2,'in'),
        array('repassword','password','两次密码输入不一致',0,'confirm'),
        array('user_pwd','checkPwd','用户密码不正确',0,'function'),
        array('user_name','/^[a-z]\w{3,}$/i','用户名不能为空')
    );
    /*检查账户可用*/
    public function chkAccountAvai(){
        $User = D("p_user");
        if (!$User->create()){
            exit($User->getError());
        }else{
            $map["user_name"]=I("user_name");
            $map["user_pwd"]=md5(I("user_pwd"));
            $data = $User->where($map)->find();
            session("userName",I("user_name"));
            session("userId",$data['id']);
            return $data;
        }
    }
    /*获取所有的推送*/
    public function getAllPosters(){
        $data = $this->table("p_poster")->order("create_time desc")->select();
        return $data;
    }
    /*保存推送*/
    public function savePoster(){
        $User = D("p_poster");
        $id = I('post_id');
        $data['post_title']=I('post_title');
        $data['post_content']=I('post_content');
        $data['status']=(int)I('status');
        $data['post_desc']=I('post_desc');
        $data['create_by']='admin';
        $data['post_call']=I('post_call');
		$data['post_imgurl']=I('post_imgurl');
        $data['create_time']=date("Y:m:d H:i:s");
        if(!empty($id)){
            $data['post_id']=$id;
            $res = $User->save($data);
            return $res;
        }
        $res = $User->add($data);
        return $res;
    }
    /*获取一条推送数据*/
    public function getOneById($id){
        $poster = D('p_poster');
        $res = $poster->find($id);
        return $res;
    }
    /*删除一条或多条记录*/
    public function deleteByIds($idStr){
        $poster = D('p_poster');
        $res = $poster->where("post_id IN('".$idStr."')")->delete();
        return $res;
    }
}